/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.cloudminga.gui;

/**
 *
 * @author jyoti
 */
import java.io.File;
import javax.swing.table.DefaultTableModel;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

//code will parse the backupsetview.xml file and print its elements to the console.
public class XmlFavReader {

DefaultTableModel dtm;

public void xmlRead()
{
  try {
  //File file = new File("Xmlfile\\backupview.xml");/*path of the xml file*/

  DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

  DocumentBuilder db = dbf.newDocumentBuilder();

  Document doc = db.parse(new File("Xmlfile\\backupviewfav.xml"));

  doc.getDocumentElement().normalize();

  NodeList nodeLst = doc.getElementsByTagName("fileproperty");
//  System.out.println("Information of all files");
//  System.out.println(nodeLst.getLength());

/* for loop begins here */
  for (int s = 0; s < nodeLst.getLength(); s++) {

    Node fstNode = nodeLst.item(s);

    if (fstNode.getNodeType() == Node.ELEMENT_NODE) {
     /*to get the node as filename */
      Element fstElmnt = (Element) fstNode;
      NodeList fileNameList = fstElmnt.getElementsByTagName("filename");
      Element fileNmelmt = (Element) fileNameList.item(0);
      NodeList fileName = fileNmelmt.getChildNodes();
      System.out.println("File Name : "  + ((Node) fileName.item(0)).getNodeValue());

     /*to get the node as filepath */
      NodeList filePathelmntLst = fstElmnt.getElementsByTagName("filepath");
      Element filePathelmnt = (Element) filePathelmntLst.item(0);
      NodeList filePath = filePathelmnt.getChildNodes();
      System.out.println("Path  : " + ((Node) filePath.item(0)).getNodeValue());

     /*to get the node as filesize*/
      NodeList fileSizeLst =fstElmnt.getElementsByTagName("filesize");
      Element fileSizeelmt = (Element) fileSizeLst.item(0);
      NodeList fileSize = fileSizeelmt.getChildNodes();
      System.out.println("File Size : "  + ((Node) fileSize.item(0)).getNodeValue());

     /*to get the node as lastmodified*/
      NodeList fileModifiedLst =fstElmnt.getElementsByTagName("LastModified");
      Element fileModifiedelmt = (Element) fileModifiedLst.item(0);
      NodeList fileModified = fileModifiedelmt.getChildNodes();
      System.out.println("File Modified : "  + ((Node) fileModified.item(0)).getNodeValue());

      /* to get the values in bacupsetViewFileTable */

      dtm = (DefaultTableModel) MainForm.bacupsetViewFileTable.getModel();
      dtm.getDataVector().removeAllElements();
      //  MainForm.bacupsetViewFileTable.getModel().setValueAt(fileName,k , 0);
      Object [] obj={null,((Node) fileName.item(0)).getNodeValue(),((Node) filePath.item(0)).getNodeValue(),((Node) fileSize.item(0)).getNodeValue(),((Node) fileModified.item(0)).getNodeValue(),};
      dtm.addRow(obj);

      }

    }
  } catch (SAXParseException err) {
        System.out.println ("Parsing error" + ", line "
             + err.getLineNumber () + ", uri " + err.getSystemId ());
        System.out.println(" " + err.getMessage ());

        }catch (SAXException e) {
        Exception x = e.getException ();
        ((x == null) ? e : x).printStackTrace ();

        }catch (Throwable t) {
        t.printStackTrace ();
        }


}

}